package com.numberone.master.modules.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.numberone.master.common.utils.Collections3;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.modules.sys.entity.Complain;
import com.numberone.master.modules.sys.entity.Skill;
import com.numberone.master.modules.sys.service.ComplainService;
import com.numberone.master.modules.sys.service.SkillService;
import com.numberone.master.modules.sys.utils.UserUtils;


@Controller
@RequestMapping(value = "${adminPath}/sys/skill")
public class SkillController extends BaseController {
	
	@Autowired
	private SkillService skillService;
	@Autowired
	private ComplainService complainService;
	
	@ModelAttribute("skill")
	public Skill get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return skillService.get(id);
		}else{
			return new Skill();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Skill skill, Model model) {
		List<Skill> list = skillService.findAllList(skill);
		for (Skill s : list) {
			List<Complain> complainList = complainService.findListBySkillId(s.getId());
			String complainNames = Collections3.extractToString(complainList, "name", ",");
			s.setComplainNames(complainNames);
		}
		model.addAttribute("list", list);
		return "modules/sys/skillList";
	}
	
	@RequestMapping(value = "save")
	public String save(Skill skill, Model model) {
		List<Skill> list = UserUtils.getAllSkill();
		for (Skill s : list) {
			if(skill.getName().equals(s.getName())){
				addMessage(model,"技能"+skill.getName()+"已存在！");
				return form(skill, model);
			}
		}
		skillService.save(skill);
		skill.setId(skillService.getByName(skill.getName()).getId());
		skillService.saveSkillComplain(skill);
		return "redirect:" + adminPath + "/sys/skill/list?repage";
	}
	
	@RequestMapping(value = "form")
	public String form(Skill skill, Model model) {
		List<Complain> complainList = complainService.findListBySkillId(skill.getId());
		List<String> complainIdList = Lists.newArrayList();
		for (Complain c : complainList) {
			complainIdList.add(c.getId());
		}
		String complainNames = Collections3.extractToString(complainList, "name", ",");
		skill.setComplainNames(complainNames);
		skill.setComplainIdList(complainIdList);
		model.addAttribute("skill", skill);
		return "modules/sys/skillForm";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Skill skill, Model model) {
		skillService.delete(skill);
		return "redirect:" + adminPath + "/sys/skill/list?repage";
	}
	
	@ResponseBody
	@RequestMapping("treeData")
	public List<Map<String, Object>> treeDate(Skill skill, Model model,HttpServletResponse response) {
		List<Skill> list = skillService.findAllList(skill);
		List<Map<String, Object>> mapList = Lists.newArrayList();
		for (Skill s : list) {
			/*List<Complain> complainList = complainService.findListBySkillId(s.getId());
			String complainNames = Collections3.extractToString(complainList, "name", ",");
			s.setComplainNames(complainNames);*/
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", s.getId());
			map.put("no", s.getNo());
			map.put("pId", "");
			map.put("pIds", "");
			map.put("name", s.getName());
		
			mapList.add(map);
		}
		//model.addAttribute("list", list);
		return mapList;
	}

}
